import React, {PureComponent} from "react"
import ImageLoader from 'react-imageloader'
import {defaultAvatar} from "../../init"
import {HOST} from "../../project-config"

export default class AvatarLoader extends PureComponent {
  render() {
    const {src, style, ...other} = this.props

    let avatarSrc
    if (!src) {
      avatarSrc = defaultAvatar
    } else if (src.indexOf('http://') === 0 || src.indexOf('https://') === 0) {
      avatarSrc = src
    } else {
      avatarSrc = `${HOST}${src}`
    }

    return <ImageLoader src={avatarSrc} {...other} {...style}>
      <div>
        <img alt={''} {...style} src={defaultAvatar}/>
      </div>
    </ImageLoader>
  }
}
